package com.hp.his.basic.web;

import java.io.PrintWriter;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.hp.his.basic.entity.ExamDetail;
import com.hp.his.basic.entity.Question;
import com.hp.his.basic.entity.User;
import com.hp.his.basic.service.UPMSBaseManager;
import com.hp.his.core.util.ServiceException;
import com.hp.his.core.util.web.struts2.CrudActionSupport;
import com.opensymphony.xwork2.ActionContext;

@Namespace("/exam")
@Results({    
       @Result(name = CrudActionSupport.RELOAD, location = "maintain.action", type = "redirect"),
       @Result(name = "EXAM_LIST", location = "list.action", type = "redirect"),
})
public class MaintainAction extends  CrudActionSupport<ExamDetail> {
	private Integer examId;
	private ExamDetail exam;
	
	
	@Autowired
	private UPMSBaseManager baseManager;


	@Override
	public String list() throws Exception {
		if(examId!=null){
			exam = baseManager.getExamById(examId);
		}else{
			exam = new ExamDetail();
		}	
		
		return SUCCESS;
	}

	@Override
	public String input() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String add() throws Exception{
		HttpServletResponse response = ServletActionContext.getResponse(); 
		response.setCharacterEncoding("UTF-8");      
		response.setContentType("text/plain");  
		PrintWriter out = response.getWriter();  
		
		ActionContext context = ActionContext.getContext();
		Map session = context.getSession();  
		
		String jasonStr = "";
		
		exam.setCreatorId( ((User)session.get("loggedUser")).getUserId() );
		exam.setQuestions( new HashSet<Question>());
		
		try{
			baseManager.saveExam(exam);
			jasonStr = "{'status':0,'examId':" +exam.getExamId()+
					"}";			
		} catch (ServiceException se){
			addActionMessage(se.getMessage());
			jasonStr = "{'status':-1}";
		}	
		
		out.println(jasonStr);   
		out.flush();
		out.close();
		
		
		return null;
	}

	@Override
	public String delete() throws Exception {
		ExamDetail exam = baseManager.getExamById(examId);
		baseManager.deleteExam(exam);
		return "EXAM_LIST";
	}

	@Override
	protected void prepareModel() throws Exception {
		// TODO Auto-generated method stub
		
	}	

	@Override
	public ExamDetail getModel() {
		// TODO Auto-generated method stub
		return exam;
	}

	public ExamDetail getExam() {
		return exam;
	}

	public void setExam(ExamDetail exam) {
		this.exam = exam;
	}

	public Integer getExamId() {
		return examId;
	}

	public void setExamId(Integer examId) {
		this.examId = examId;
	}

	@Override
	public String save() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

}
